package com.turing.usdtpay.manager;

import android.app.Activity;
import android.content.Context;

import com.google.gson.Gson;
import com.turing.usdtpay.common.SPKeyConstants;
import com.turing.usdtpay.entity.LoginData;
import com.turing.usdtpay.entity.UserData;
import com.turing.usdtpay.utils.LogUtils;
import com.turing.usdtpay.utils.SPHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by $Jay
 * Date : 2019\1\25 0025
 **/
public class UserManager {

    private LoginData mLoginData = null;
    private UserData mUserData = null;
    private static UserManager mInstance;
    private Gson mGson = new Gson();
    private SPHelper mSpHelper  ;
    private SPHelper configHelper ;

    private UserManager() {
        mSpHelper = new SPHelper(SPKeyConstants.TR_SDK_ACCOUNT_LIST);
        configHelper = new SPHelper(SPKeyConstants.TR_SDK_CONFIG);
    }

    public static UserManager getInstance() {
        synchronized (UserManager.class) {
            if (mInstance == null) {
                mInstance = new UserManager();
            }
        }
        return mInstance;
    }

    public LoginData saveLoginInfo(LoginData date) {
        mLoginData = date;
        return mLoginData;
    }


    public UserData saveUserDate(String data) {
        mUserData = mGson.fromJson(data, UserData.class);
        return mUserData;
    }

    public LoginData getLoginDate() {
        return mLoginData;
    }

    public UserData getUserDate() {
        return mUserData;
    }

    public ArrayList<UserData> getUserList() {
        ArrayList<UserData> mUserList = new ArrayList<>();
        Map<String, String> map = (Map<String, String>) mSpHelper.getAll();
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            String account = (String) entry.getKey();
            String pwd = (String) entry.getValue();
            mUserList.add(new UserData(account, pwd));
        }
        return mUserList;
    }

    /**
     * 保存输入成功的账号
     *
     * @param account
     */
    public void saveAccount(String account, String pwd) {
        mSpHelper.put(account, pwd);
        configHelper.put(SPKeyConstants.TR_SDK_LATE_ACCOUNT,account);
    }

    public void saveInfo(String account,String uid,String verify){
        configHelper.put(SPKeyConstants.PARAM_KEY_ACCOUNT,account);
        configHelper.put(SPKeyConstants.PARAM_KEY_UID,uid);
        configHelper.put(SPKeyConstants.PARAM_KEY_VERIFY,verify);
    }

    public void removeUser(String name) {
        if (mSpHelper.contains(name)) {
            mSpHelper.remove(name);
            LogUtils.d("removeUser before,remove  -- >" + name);
        }
    }

    /**
     * 根据用户名获取用户信息
     * @param name
     * @return
     */
    public UserData getCurrentAccount(String name) {
        UserData user = null;
        Map<String, String> map = (Map<String, String>) mSpHelper.getAll();
        if (map != null) {
            Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> next = iterator.next();
                if (next.getKey().equals(name)){
                    user = new UserData(next.getKey(),next.getValue());
                    break;
                }
            }
        }
        return user;
    }

    public String getLastUser(){
        return configHelper.getString(SPKeyConstants.TR_SDK_LATE_ACCOUNT, "");
    }
}
